package com.base.system.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.annotation.SelectItems;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.common.provider.SecuritySelectItemProvider;
import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.DepartmentalUnit;
import com.base.organization.service.IDepartmentalUnitDAO;
import com.base.organization.service.IOrganizationService;
import com.base.security.SecurityConst;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_departmentalUnitListBean", scope = ManagedBeanScope.REQUEST)
public class DepartmentalUnitListBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(DepartmentalUnitListBean.class);
	private IDepartmentalUnitDAO departmentalUnitDAO = SpringBeanHolder
			.getBean("departmentalUnitDAO");
	private IOrganizationService organizationService = SpringBeanHolder
			.getBean("orgService");
	@Inject
	private FacesContext context;
	@SaveState
	@Bind(id = "code", attribute = "value")
	private String code;
	@SaveState
	@Bind(id = "departmentName", attribute = "value")
	private String departmentName;
	@Bind(id = "departmentalUnitGrid", attribute = "binding")
	private UIDataGrid departmentalUnitGrid;

	@DataModel(id = "departmentalUnitGrid")
	private List<DepartmentalUnit> getDeps() {
		if ("".equals(code) && "".equals(departmentName)
				&& "".equals(belongCentralUnit))
			return organizationService.getAllDepartment();
		List data = new ArrayList();
		List lst = new ArrayList();
		String sql = "select cu from DepartmentalUnit cu where (1=1)";
		if (code != null && !"".equals(code)) {
			sql += " and cu.departmentCode like '%" + code + "%'";
		}
		if (departmentName != null && !"".equals(departmentName)) {
			sql += " and cu.name like '%" + departmentName + "%'";
		}
		List list = departmentalUnitDAO.findByHql(sql);
		if (belongCentralUnit != null && !"".equals(belongCentralUnit)) {
			data.clear();
			lst = organizationService.getChildDepartmentByCentralUnit(Integer
					.valueOf(belongCentralUnit));
			for (int i = 0; i < list.size(); i++) {
				DepartmentalUnit duSearch = (DepartmentalUnit) list.get(i);
				for (int j = 0; j < lst.size(); j++) {
					DepartmentalUnit du = (DepartmentalUnit) lst.get(j);
					if (duSearch.equals(du)) {
						data.add(duSearch);
						break;
					}
				}
			}
		} else
			data = list;
		return data;
	}

	@Accessible
	DepartmentalUnit departmentalUnit;
	@Accessible
	private SelectItem[] belongCentralUnit_items = SecuritySelectItemProvider
			.getAllCentralUnit(false);
	@SaveState
	@Bind(id = "belongCentralUnit")
	@SelectItems(source = "#{this.belongCentralUnit_items}")
	private String belongCentralUnit;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		if (!isPostback) {
			// data = organizationService.getAllDepartment();
			code = null;
			departmentName = null;
			belongCentralUnit = null;
		}
	}

	@Action(id = "query")
	public void query() {
		departmentalUnitGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		code = null;
		belongCentralUnit = null;
		departmentName = null;
		// data = departmentalUnitDAO.getAll();
		departmentalUnitGrid.reload();
	}

	private boolean initSelected() {
		Object[] objs = departmentalUnitGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				SecurityConst.DEPARTMENTALUNIT_SELECT_ROW_DATA,
				(DepartmentalUnit) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	public void showDepartmentalUnit() {
		if (initSelected()) {
			String url = "/modules/system/showDepartmentalUnit.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.DEPARTMENTALUNIT_SHOW_TAB + "','" + "查看部门"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "editDepartmentalUnit")
	public void editDepartmentalUnit() {
		if (initSelected()) {
			String url = "/modules/system/editDepartmentalUnit.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.DEPARTMENTALUNIT_EDIT_TAB + "','" + "编辑部门"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "deleteDepartmentalUnit")
	public void deleteDepartmentalUnit() {
		Object[] objs = departmentalUnitGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			return;
		}
		departmentalUnitDAO.delete((DepartmentalUnit) objs[0]);
		departmentalUnitGrid.reload();
		// Browser.execClientScript("window.alert('删除成功！');");
	}
}
